package com.jstyle.jclifexd.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.request.RequestOptions;
import com.jstyle.jclifexd.R;
import com.jstyle.jclifexd.model.Createddata;
import com.jstyle.jclifexd.model.JoinUserData;
import com.jstyle.jclifexd.utils.ScreenUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class DetailsAdapter extends RecyclerView.Adapter<BaseViewHolder> {
    List<JoinUserData>  Details;
    Context context;
    //私有属性
    private OnItemClickListener onItemClickListener = null;

    public interface OnItemClickListener {
        void onItemClick(Createddata aa, int position);
    }

    //setter方法
    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public DetailsAdapter(Context mycontext) {
        this.context = mycontext;
        this.Details = new ArrayList<>();
    }

    public void SetData(List<JoinUserData> myDetails) {
        this.Details = myDetails;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public BaseViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        return new ViewHolder(LayoutInflater.from(context).inflate(R.layout.item_details, viewGroup, false));
    }

    @Override
    public void onBindViewHolder(final BaseViewHolder holder, final int position) {
        final JoinUserData sleepDetail = Details.get(position);
        final ViewHolder viewHolder = (ViewHolder) holder;
        Glide.with(context)
                .load(sleepDetail.getAvatar()).dontAnimate()
               // .skipMemoryCache(true)// 不使用内存缓存
                .override(ScreenUtils.dip2px(context,40),ScreenUtils.dip2px(context,40))
                .diskCacheStrategy(DiskCacheStrategy.DATA) // 不使用磁盘缓存
                .placeholder(context.getResources().getDrawable(R.drawable.rank_top))
                .apply(RequestOptions.bitmapTransform(new CircleCrop()))
                .error(context.getResources().getDrawable(R.drawable.rank_top))
                .into(viewHolder.imageview);
        viewHolder.title_name.setText(sleepDetail.getNickname());

        if(sleepDetail.getTag().equals("发起者")){
            viewHolder.initiator.setVisibility(View.VISIBLE);
        }else{
            viewHolder.initiator.setVisibility(View.INVISIBLE);
        }


    }

    @Override
    public int getItemCount() {
        return null == Details ? 0 : Details.size();
    }


    static class ViewHolder extends BaseViewHolder {
        @BindView(R.id.img_cearted)
        ImageView imageview;
        @BindView(R.id.title_name)
        TextView title_name;
        @BindView(R.id.initiator)
        TextView initiator;

        ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }

}
